<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/3/10
 * Time: 10:50
 */

require_once 'db.php';
require_once 'response.php';

header('content-type:text/html:charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

$page = isset($_GET['page']) ? $_GET['page'] : '';
$pageSize = isset($_GET['pageSize']) ? $_GET['pageSize'] : '';

if(!is_numeric($page) || !is_numeric($pageSize)) {
    return Response::show(401,'数据不合法');
}

try{
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

$offset = ($page-1)*$pageSize;

$sql = "SELECT news_id,news_title,news_time,news_maker FROM news LIMIT {$offset},{$pageSize}";

$res = $connect->query($sql);

$data = array();

if($res && $res->num_rows>0) {
    while($info = $res->fetch_assoc()){
        $data[] = $info;
    }
    $res->free_result();
    $sql = "SELECT COUNT(*) FROM news";
    $res = $connect->query($sql);
    $num = $res->fetch_row();
    return Response::show(200,$num[0],$data);
} else {
    return Response::show(404,'没有获取到新闻列表');
}




